package day25;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

//用户系统  通过序列化来保存数据
public class UserSystem {
    //保存所有被注册过的账号
    static List<User> list=new ArrayList<>();
    //保存当前登录成功用户信息
    static User userLogin;
    static User userCashing = null;
    static Scanner sc=new Scanner(System.in);
    static Boolean flag=true;
    static File f = new File("d://io//users.yy");
    //因为它是最先执行的 而且只有一次  去做反序列化(来初始化集合)
    static{
        //反序列化
        input(flag);
    }
    public static void main(String[] args) {
        ScheduledExecutorService ses
                = Executors.newScheduledThreadPool(1);
        ses.scheduleAtFixedRate(()->{
            System.out.println("正在刷新缓存");
            if(f.exists()){
                userCashing=null;
                f.delete();
            }
        },0,60, TimeUnit.SECONDS);
        one();
    }
    public static void one(){
        while(true) {
            System.out.println("****XX用户系统****");
            System.out.println("1.用户登录");
            System.out.println("2.用户注册");
            System.out.println("3.退出系统");
            System.out.println("请输入你要选择的编号:");
            System.out.println("*****************");
            int n = sc.nextInt();
            switch (n) {
                case 1: login();break;
                case 2: reg();break;
                case 3: System.out.println("退出用户系统");return;
            }
        }
    }
    public static void login(){
        int n = 0;
        while (true) {
            System.out.println("是否使用缓存:y/n");
            if (sc.next().charAt(0) == 'y') {
                input(false);
                userLogin = userCashing;
                if (userCashing !=null){
                    System.out.println("ok");
                    two();
                    return;
                }
            } else {
                System.out.println("输入登录的账号");
                String un = sc.next();
                System.out.println("输入登录的密码");
                String pw = sc.next();
                for (User u : list) {
                    if (u.getUsername().equals(un) && u.getPassword().equals(pw)) {
                        userLogin = u;
                        System.out.println("是否保存用户账号,密码以便下次快捷登录y/n");
                        if(sc.next().charAt(0) == 'y') {
                            userCashing = new User();
                            userCashing.setUsername(userLogin.getUsername());
                            userCashing.setPassword(userLogin.getPassword());
                            output(false);
                        }
                        two();
                        return; //二级菜单
                    }
                }
            }
            n++;
            System.out.println("您还有" + (3 - n) + "机会");
            if (n == 3) {
                System.exit(0);
            }
        }
    }
    public static void reg(){
        System.out.println("请输入注册账号:");
        String username=sc.next();
        System.out.println("请输入注册密码:");
        String password=sc.next();
        System.out.println("请输入注册年龄:");
        int age=sc.nextInt();
        System.out.println("请输入注册城市:");
        String city=sc.next();
        User u=new User(username,password,age,city);
        list.add(u);
        //序列化保存到本地
        output(flag);
    }
    public static void two(){
        System.out.println(userLogin.getUsername()+"账号登录成功");
        while(true){
            System.out.println("*********");
            System.out.println("1.查询用户");
            System.out.println("2.修改密码");
            System.out.println("3.删除用户");
            System.out.println("4.新增用户");
            System.out.println("5.退出登录");
            System.out.println("*********");
            int n=sc.nextInt();
            switch (n){
                case 1: select();break;
                case 2: update();break;
                case 3: delete();break;
                case 4: reg();break;
                case 5: return;
            }
        }
    }
    public static void select(){
        System.out.println("账号\t\t密码\t\t年龄\t\t城市");
        for(User u:list){
            System.out.println(u.getUsername()+
                    "\t"+u.getPassword()+
                    "\t"+u.getAge()+
                    "\t"+u.getCity());
        }
    }
    public static void update(){
        System.out.println("请输入你修改后的密码");
        String pw1=sc.next();
        System.out.println("请再次输入你修改后的密码");
        String pw2=sc.next();
        if(pw1.equals(pw2)){
            for(User u:list){
                if (u.getUsername().equals(userLogin.getUsername())){
                    u.setPassword(pw1); output(flag); break;
                }
            }
        }else{
            System.out.println("两次输入的不一致");
        }
    }
    public static void delete(){
        System.out.println("请输入删除的账号");
        String un=sc.next();
        list.removeIf(u->u.getUsername().equals(un));
        output(flag);
    }

    public static void output(Boolean flag){//序列化
        try (FileOutputStream fos = new FileOutputStream(flag ? "d://io//users.xx" : "d://io//users.yy");
             ObjectOutputStream oos = new ObjectOutputStream(fos)) {
            if(flag){
                oos.writeObject(list);
            }else {
                oos.writeObject(userCashing);
            }
            System.out.println("***集合保存成功***");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static void input(Boolean flag){ //反序列化
        //第一次文件不存在的 不要自己创建 否则你创建的空文件 不能正常做序列化 EOFException
        try(FileInputStream fis=new FileInputStream(flag ? "d://io//users.xx" : "d://io//users.yy");
            ObjectInputStream ois=new ObjectInputStream(fis)){
            if(flag) {
                list = (List<User>) ois.readObject();
            }else {
                userCashing = (User) ois.readObject();
            }
            System.out.println("***初始化集合成功***");
        }catch (FileNotFoundException e){
            if(flag) {
                System.out.println("欢迎你第一次使用");
            }else {
                System.out.println("缓存为空");
            }
        } catch (IOException e){
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}
class User implements Serializable {
    private String username;
    private String password;
    private int age;
    private String city;
    public User(String username, String password, int age, String city) {
        this.username = username;
        this.password = password;
        this.age = age;
        this.city = city;
    }
    public User() {
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }
}



